package com.cj.MyHttp;

import com.cj.MyHelper.ServiceHelper;
import com.cj.MyHelper.ServiceUtil;
import com.cj.models.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class MyHttp {

    @Autowired
    ServiceHelper serviceHelper;
    @RequestMapping(value = "/{serviceName:\\w+}/{action:[a-z]+}/{methodName:\\w+}",method  = {RequestMethod.GET,RequestMethod.POST})
    public ResultModel forGetByAction(@PathVariable("serviceName") String sname,
                                      @PathVariable("action") String action,
                                      @PathVariable("methodName") String mname, HttpServletRequest httpRequest) throws Exception {
        String methodName=action.toLowerCase()+
                ServiceUtil.ucfirst(ServiceUtil.camelCase(mname.toLowerCase()));

        Object ret=this.serviceHelper.invoke(sname,methodName,httpRequest);
        return new ResultModel("success",ret);
    }
    @RequestMapping(value = "/{serviceName:\\w+}/{methodName:\\w+}",method = {RequestMethod.GET,RequestMethod.POST})
    public Object forGet(@PathVariable("serviceName") String sname,
                              @PathVariable("methodName") String mname, HttpServletRequest httpRequest) throws Exception {

       return this.serviceHelper.
                invoke(sname,mname.toLowerCase(),httpRequest);

    }


}
